﻿using System;

namespace Maple.Weixin.MP.Entities.Messages
{
    /// <summary>
    /// 消息总线。主要负责消息与所对应的服务的映射。
    /// </summary>
    public class MessageBus : IMessageBus
    {
        /// <summary>
        /// 根据消息找到其对应的服务。
        /// </summary>
        /// <param name="requestMessage">所请求的消息。</param>
        /// <returns>与消息对应的服务。</returns>
        IResponseService IMessageBus.FindService(RequestMessageBase requestMessage) {
            switch (requestMessage.MsgType) {
                case RequestMessageType.Text:
                    return OnTextRequest((RequestMessageText) requestMessage);
                case RequestMessageType.Image:
                    return OnImageReqeust((RequestMessageImage) requestMessage);
                case RequestMessageType.Link:
                    return OnLinkRequest((RequestMessageLink) requestMessage);
                case RequestMessageType.Location:
                    return OnLocationRequest((RequestMessageLocation) requestMessage);
                case RequestMessageType.Video:
                    return OnVideoRequest((RequestMessageVideo) requestMessage);
                case RequestMessageType.Voice:
                    return OnVoiceRequest((RequestMessageVoice) requestMessage);
                case RequestMessageType.Event:
                    return OnEventRequest((RequestMessageEventBase) requestMessage);
                default:
                    throw new NotSupportedException();
            }
        }

        protected virtual IResponseService OnEventRequest(RequestMessageEventBase requestMessage) {
            switch (requestMessage.Event)
            {
                case Event.LOCATION:  //自动发送的用户当前位置
                    return OnEventLocationRequest((RequestMessageEventLocation) requestMessage);
                case Event.subscribe:  //订阅
                    return OnEventSubscribeRequest((RequestMessageEventSubscribe) requestMessage);
                case Event.unsubscribe:  //退订
                    return OnEventUnsubscribeRequest((RequestMessageEventUnsubscribe) requestMessage);
                case Event.CLICK:  //菜单点击
                    return OnEventClickRequest((RequestMessageEventClick) requestMessage);
                case Event.scan:  //二维码
                    return OnEventScanRequest((RequestMessageEventScan) requestMessage);
                //case Event.TEMPLATESENDJOBFINISH:
                //    return OnEventTemplateSendJobFinishRequest((RequestMessageEventTemplateSendJobFinish) requestMessage);
                case Event.VIEW:  //URL跳转（view视图）
                    return OnEventViewRequest((RequestMessageEventView) requestMessage);
                default:
                    throw new NotSupportedException();
            }
        }

        protected virtual IResponseService OnEventViewRequest(RequestMessageEventView requestMessage) {
            return DefaultService(requestMessage);
        }

        protected virtual IResponseService OnEventScanRequest(RequestMessageEventScan requestMessage) {
            return DefaultService(requestMessage);
        }

        protected virtual IResponseService OnEventClickRequest(RequestMessageEventClick requestMessage) {
            return DefaultService(requestMessage);
        }

        protected virtual IResponseService OnEventUnsubscribeRequest(RequestMessageEventUnsubscribe requestMessage) {
            return DefaultService(requestMessage);
        }

        protected virtual IResponseService OnEventSubscribeRequest(RequestMessageEventSubscribe requestMessage) {
            return DefaultService(requestMessage);
        }

        //protected virtual IResponseService OnEventTemplateSendJobFinishRequest(
        //    RequestMessageEventTemplateSendJobFinish requestMessage) {
        //    return DefaultService(requestMessage);
        //}

        protected virtual IResponseService OnEventLocationRequest(RequestMessageEventLocation requestMessage) {
            return DefaultService(requestMessage);
        }

        protected virtual IResponseService OnVoiceRequest(RequestMessageVoice requestMessage) {
            return DefaultService(requestMessage);
        }

        protected virtual IResponseService OnVideoRequest(RequestMessageVideo requestMessage) {
            return DefaultService(requestMessage);
        }

        protected virtual IResponseService OnLocationRequest(RequestMessageLocation requestMessage) {
            return DefaultService(requestMessage);
        }

        protected virtual IResponseService OnLinkRequest(RequestMessageLink requestMessage) {
            return DefaultService(requestMessage);
        }

        protected virtual IResponseService OnTextRequest(RequestMessageText requestMessage) {
            return DefaultService(requestMessage);
        }

        protected virtual IResponseService OnImageReqeust(RequestMessageImage requestMessage) {
            return DefaultService(requestMessage);
        }

        protected virtual IResponseService DefaultService(RequestMessageBase requestMessage) {
            return new ResponseText("Hello, Maple!");
        }
    }
}
